/**
 * Created by Administrator on 2017/3/30.
 */
app.controller('detailActCtr',
    ['$scope', '$location', '$http', '$rootScope', '$routeParams', '$sce', function ($scope, $location, $http, $rootScope, $routeParams, $sce) {
        var articleId = $routeParams.fileId;

        console.log("articleId");
        console.log(articleId);

        //活动规则展开功能
        $scope.lunchActList=function(){
            $("#actDetailRul").removeClass('actDetailRulHide');
            $(".lunchImg").css({display:'none'});
        }

        ////最近参与名单
        //$http.jsonp(activityEntryList+'fileId='+articleId+'&siteId=1'+'&jsoncallback=JSON_CALLBACK'
        //).success(function (data, header, config, status) {
        //    console.log("参与人名单");
        //    $scope.activNameList=data;
        //    $scope.activNameLists=data.list;
        //}).error(function (data, header, config, status) {
        //    console.log("参与人名单失败");
        //});
        var topic="";
        // articleId=测试155

            $http.jsonp(DetailActData+'siteID=1&fileId='+articleId+'&jsoncallback=JSON_CALLBACK'
            ).success(function (data) {
                debugger
                $scope.actDetailData = data;
                console.log("活动详情");
                topic=data.title;//评论使用
                console.log($scope.actDetailData);
                var datas=data.content;//获取动态模板
                var indexA=datas.indexOf("<body>");
                var indexB=datas.indexOf("<\/body>");
                var detailDatass=datas.substring((indexA+6) ,indexB);
                //后去除去body标签内容
                $scope.conAct=detailDatass;

                $scope.trustHtmlAct = $sce.trustAsHtml($scope.conAct);
                if(indexA==-1){
                    $scope.trustHtmlAct = $sce.trustAsHtml(datas);
                }
                if(!$scope.conAct){
                    $scope.hideDetailDiv=true;
                    $scope.hideShowAllArt=true;
                }
            }).error(function (data, header, config, status) {
            });

      /*         //获取最新评论  page=0开始;加载更多评论
        var compage = 0;
        $scope.LoadMoreCom = function () {
            compage++;
            console.log("下页" + compage);
            $scope.getNewCom();
        };

        //获取最新评论
        $scope.getNewCom = function () {
            console.log(compage);
            $http.jsonp(articleCommentDataUrl+'id=' + articleId + '&siteId=1&lastFileId=&page=' + compage + '&jsoncallback=JSON_CALLBACK'
            ).success(function (data, header, config, status) {
                console.log(data);
                // 是否还有更多评论
                $scope.hasMorCom = data.hasMore;
                $scope.commenCon = data.list;
            }).error(function (data, header, config, status) {

            });
        };

        $scope.getNewCom();
        $scope.getHotCom = function () {
            $http.jsonp(HotCommentDetailUrl+'id=' + articleId + '&siteID=&jsoncallback=JSON_CALLBACK'
            ).success(function (data, header, config, status) {
                // 是否还有更多评论
                $scope.hasMorCom = data.hasMore;
                //获取评论
                $scope.commenCon = data.list;
            }).error(function (data, header, config, status) {
            });
        };

        //回复评论
        $scope.replayCom = function (parentId, userName, ParentUserID) {
            // 提交评论使用
            $scope.parentId = parentId;
            $scope.userName = userName;
            $scope.ParentUserID = ParentUserID;
            $("#comInput").attr("placeholder", "@" + userName + ":");
        };
        // 发送评论，地址和登录的id未知
        $scope.sendCom = function () {
            var sendMesg = $("#comInput").val();
            var logUserId=$scope.uid_fouNder;
            var LogUserName=$scope.username_fouNder;

            var logUserId = 10;
            var LogUserName = 'sss';
            var siteID = 1, rootID = articleId, parentID = $scope.parentId, parentUser = $scope.userName,
                parentUserID = $scope.ParentUserID, topic = $scope.detailData.title, userID = logUserId
                , userName = LogUserName, type = 0, content = sendMesg;


            //提交回复
            $.ajax({
                type: "post",
                url: sendDiscussData,
                data: {
                    "siteID": siteID,
                    "rootID": '3969',
                    "parentID": parentID,
                    "parentUser": parentUser,
                    "parentUserID": parentUserID,
                    "topic": topic,
                    "userID": userID,
                    "userName": userName,
                    "type": type,
                    "content": content,
                    "channel": 1
                },
                dataType: "jsonp",
                jsonp: "jsoncallback",
                jsoncallback: "jsoncallback",
                success: function (data) {
                    if (data) {
                        console.log(data);
                        alert("评论成功！重新获取数据获取不到");
                        $scope.getNewCom();
                        console.log("发送内容为：");
                        console.log(sendMesg);
                        $("#comInput").val("");
                    } else {
                        alert("评论失败！1");
                    }

                },
                error: function () {
                    alert("评论失败！2");
                }
            });

        };*/
        /**
         * 活动ource=6 活动视频直播完全相同部分
         * */
        /*   //获取最新评论  page=0开始;加载更多评论以下和detail评论部分完全相同
         var compage=0;
         $scope.LoadMoreCom=function () {
         compage++;
         console.log("下页"+compage);
         $scope.getNewCom();
         };*/

        //获取最新评论id=3969
        $scope.getNewCom=function () {
            //判断登录与否
            if($rootScope.uid_fouNder){
                $scope.commeInputs="写评论";
            }else{
                $scope.commeInputs="请登录后评论";

            }
            // console.log(compage);
            $http.jsonp(newComment+'id='+articleId+'&siteId=1&lastFileId=0&page=0&source=6&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log("新评论");
                console.log(data);
                // 是否还有更多评论
                $scope.hasMorCom=data.hasMore;
                $scope.newCommenCon=data.list;
                console.log($scope.newCommenCon);
                $scope.newComCounts=data.totalCount;
                console.log($scope.newComCounts);
            }).error(function(data,header,config,status){

            });
        };


        //获取 热门评论'+articleId+' 测试3969
        $scope.getHotCom=function () {
            $http.jsonp(hotComment+'id='+articleId+'&siteId=1&source=6&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                console.log("热门评论")
                console.log(data)
                $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        };

        //首先加载热门评论
        $scope.getNewCom();
        $scope.getHotCom();

        //弹出模态框
        $scope.showModeAndRepley=function ($event) {
            if($rootScope.uid_fouNder){
                $('#commentModle').css('display',"block");
                $("#commentArea").css('display',"block");
                $("#comInput").focus();
                $("#comm_text_footer").css('display',"none");
            }else{
                window.location='http://172.19.32.122:9081/SSOv2/user/login_app';
            }
        };
        //取消模态框
        $scope.cancelCom=function () {
            $('#commentModle').css('display',"none");
            $("#commentArea").css('display',"none");
            $("#comm_text_footer").css('display',"block");
        }

        //回复评论
        $scope.replayCom=function (parentId,userName,ParentUserID) {
            $('#commentModle').css('display',"block");
            $("#commentArea").css('display',"block");
            $("#comm_text_footer").css('display',"none");
            // 提交评论使用
            $scope.parentId=parentId;
            $scope.userName=userName;
            $scope.ParentUserID=ParentUserID;
            $("#comInput").attr("placeholder","@"+userName+":");
        };

        // 发送评论，地址和登录的id未知
        $scope.sendCom=function () {
            var sendMesg=$("#comInput").val();
            // console.log(sendMesg);
            var logUserId=$rootScope.uid_fouNder;
            var LogUserName=$rootScope.username_fouNder;
            var topicTitle=$scope.actDetailData.title;
           /* var logUserId = 10;
            var LogUserName = 'sss';*/
            var siteID = 1, rootID = articleId, parentID = $scope.parentId, parentUser = $scope.userName,
                parentUserID = $scope.ParentUserID, topic = topicTitle, userID = logUserId
                , userName = LogUserName, type = 0,content=sendMesg;

            //提交回复
            $.ajax({
                type: "post",
                url: sentComent,
                data:{
                    "siteID": siteID,
                    "rootID": rootID,
                    "parentID": parentID,
                    "parentUser": parentUser,
                    "parentUserID": parentUserID,
                    "topic": topic,
                    "userID": userID,
                    "userName": userName,
                    "type": type,
                    "content":content,
                    "channel":1
                },
                dataType : "jsonp",
                jsonp: "jsoncallback",
                jsoncallback:"jsoncallback",
                success: function(data){
                    if(data){
                        console.log(data);
                        alert("评论成功！重新获取数据获取不到");
                        $scope.getNewCom();
                        console.log("发送内容为：");
                        console.log(sendMesg);
                        $("#comInput").val("");
                        $('#commentModle').css('display',"none");
                        $("#commentArea").css('display',"none");
                        $("#comm_text_footer").css('display',"block");
                    }else{
                        alert("评论失败！");

                    }

                },
                error: function(){
                    alert("评论失败！2");
                    $("#comInput").val("");
                    $('#commentModle').css('display',"none");
                    $("#commentArea").css('display',"none");
                    $("#comm_text_footer").css('display',"block");
                }
            });
        };

        //最新评论子评论
        $scope.lunchaMorChilCom=true;
        //获取最新子评论加载更多更多内容 id为每条父评论的id号
        $scope.checkMorChildCom=function (id,index) {
            $scope.lunchaMorChilCom=false;

            $http.jsonp(replayComent+'id='+id+'&page=0&siteId=1&source=6&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                //点击查看更多子评论之前
                $scope.beforLoadMorChid=$scope.newCommenCon[index].topDiscuss.list;
                //点击查看更多子评论之后
                $scope.newCommenCon[index].topDiscuss.list=data.list;

                // 是否还有更多评论
                /*console.log("是否有更多")
                 console.log(data.hasMore)*/
                $scope.hasNoChildMorCom={};
                $scope.hasChildMorCom={};
                if(data.hasMore){
                    $scope.hasChildMorCom.index=true;
                }else{
                    $scope.hasNoChildMorCom.index=true;

                }
                //获取评论
                // $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        };

        $scope.hotlunchaMorChilCom=true;
        // 热门评论子评论
        //获取子评论加载更多更多内容 id为每条父评论的id号
        $scope.hotcheckMorChildCom=function (id,index) {
            $scope.hotlunchaMorChilCom=false;

            $http.jsonp(replayComent+'id='+id+'&page=0&siteId=1&source=6&jsoncallback=JSON_CALLBACK'
            ).success(function(data,header,config,status){
                //点击查看更多子评论之前
                $scope.hotbeforLoadMorChid=$scope.commenCon[index].topDiscuss.list;
                //点击查看更多子评论之后
                $scope.commenCon[index].topDiscuss.list=data.list;

                // 是否还有更多评论
                /*console.log("是否有更多")
                 console.log(data.hasMore)*/
                $scope.hothasNoChildMorCom={};
                $scope.hothasChildMorCom={};
                if(data.hasMore){
                    $scope.hothasChildMorCom.index=true;
                }else{
                    $scope.hothasNoChildMorCom.index=true;

                }
                //获取评论
                // $scope.commenCon=data.list;
            }).error(function(data,header,config,status){
            });
        }


        //回到顶部
        $(".fiBtn").hide();
        $(window).on("scroll", function(){
            if($(window).scrollTop() >0){
                $(".fiBtn").show();
            }else{
                $(".fiBtn").hide();
            }
        });
        $scope.goBackTop=function () {
            $("body").animate({scrollTop: 0}, 300);
        };
    }]);
